# encoding:utf8
''' 2017-8-24 pylint fix '''
import os

class SvnHelper(object):

    def __init__(self, user, pwd):
        self._user = user
        self._pwd = pwd

    def svn_add(self, tarpath_):
        os.chdir(tarpath_)
        # 父路径
        cmd_add = "svn add *"
        os.system(cmd_add)

        # 子路径遍历添加
        def _iter_addfile(tarpath):
            for filename in os.listdir(tarpath):
                fileobj = os.path.join(tarpath, filename)
                if os.path.isdir(fileobj):
                    cmd_str = "svn add {}/*".format(fileobj)
                else:
                    cmd_str = "svn add {}".format(fileobj)
                os.system(cmd_str)
        _iter_addfile(tarpath_)

    def svn_commit(self, tarpath, svnlog="AUTO:Jenkins tools commit", singlefile=""):
        os.chdir(tarpath)
        cmd_commit = '''svn ci -m "{svnlog}" --username {user} --password {pwd} {singlefile}'''.format(
            svnlog=svnlog, user=self._user, pwd=self._pwd, singlefile=singlefile)
        os.system(cmd_commit)

    def svn_update(self, tarpath_):
        os.chdir(tarpath_)
        cmd_update = '''svn update --username {usr} --password {pwd} {tarpath}'''.format(
            usr=self._user, pwd=self._pwd, tarpath=tarpath_)
        print ("run the command-->>%s"%cmd_update)
        ret = os.popen(cmd_update).read()
        print (ret)